package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.mapper.MemberMapper;
import com.usian.service.IMemberService;
import com.usian.shop.entity.Member;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.shop.utils.JWTUtils;
import com.usian.shop.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-01-10
 */
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
@Autowired
private MemberMapper memberMapper;
    @Override
    public ResponseResult login(Member member) {
        if (member==null){
            return ResponseResult.errorResult(500,"登陆错误");
        }else {
            if (!StringUtils.isEmpty(member.getName()) || !StringUtils.isEmpty(member.getPassword())){
                LambdaQueryWrapper<Member> qw = new LambdaQueryWrapper<>();
                qw.eq(Member::getName,member.getName());
                Member member1 = memberMapper.selectOne(qw);
                if (member1!=null){

                    Map<String, String> map = new HashMap<>();
                    map.put("token",member1.getId()+"");
                    map.put("name",member1.getName());
                    String token = JWTUtils.getToken(map);
                    return ResponseResult.okResult(token);
                }
            }
        }
        return null;
    }
}
